package org.flower.isme.flower.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.flower.isme.common.exception.BizException;
import org.flower.isme.common.response.BizResponseCode;
import org.flower.isme.common.response.R;
import org.flower.isme.common.service.RedisService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/action")
@Slf4j
@Tag(name = "动作")
public class ActionController {

    @Resource
    private RedisService redisService;

    private final static String ACTION_KEY = "ACTION_LIGHT:";
    private final static String SELECT_FLOWER_KEY = "SELECT_FLOWER:";
    private final static String WATERFLOWER_KEY = "WATERFLOWER:";
    private final static String FEEDFLOWER_KEY = "FEEDFLOWER:";
    private final static String WATERFLOWERTIME_KEY = "WATER_FLOWER_TIME:";
    private final static String FEEDFLOWERTIME_KEY = "FEEDFLOWER_TIME:";
    private final static String SOIL_THRESHOLD_KEY = "SOIL_THRESHOLD:";

    /**
     * 设置指令
     *
     * @return R
     */
    @GetMapping("/cmd")
    @Operation(summary = "设置指令")
    public R<Integer> LightCmd() {
        Integer value = (Integer) redisService.find(ACTION_KEY);

        // 如果当前状态为 null，初始化为 0
        if (value == null) {
            redisService.save(ACTION_KEY, 0);
            value = 0; // 更新值为 0，避免后续逻辑出错
        }

        // 点灯逻辑
        if (value == 0) {
            redisService.save(ACTION_KEY, 1); // 设置为 1，表示灯亮
            log.info("发送指令");
            return R.ok(0, "发送指令");
        }

        return R.build(new BizException(BizResponseCode.ERR_400, "发送成功"));
    }

    /**
     * 单片级使用
     *
     * @return R
     */
    @GetMapping("/on")
    @Operation(summary = "点亮")
    public R<Integer> LightOn() {
        Integer value = (Integer) redisService.find(ACTION_KEY);
        if (value == null) {
            redisService.save(ACTION_KEY, 0);
            value = 0;
        }
        if (value == 1) {
            redisService.save(ACTION_KEY, 0);
            log.info("点灯");
            return R.ok(1, "点灯成功");
        }
        return R.build(new BizException(BizResponseCode.ERR_400, "点灯失败"));
    }

    @GetMapping("/select/{id}")
    @Operation(summary = "选择植物指令")
    public R<Void> selectFlower(@PathVariable("id") Long plantId) {
        redisService.save(SELECT_FLOWER_KEY, plantId);
        return R.ok();
    }

    /**
     * 获取植物id
     *
     * @return R
     */
    @GetMapping("/getflower")
    @Operation(summary = "获取植物id")
    public R<Integer> getFlowerId() {
        Integer value = (Integer) redisService.find(SELECT_FLOWER_KEY);
        return R.ok(value);
    }

    /**
     * 设置浇花指令
     *
     * @return R
     */
    @GetMapping("/set/water/flower/cmd")
    @Operation(summary = "设置浇花指令")
    public R<Void> setWaterFlowerCMD() {
        Integer value = (Integer) redisService.find(WATERFLOWER_KEY);
        if (value == null) {
            redisService.save(WATERFLOWER_KEY, 0);
            value = 0;
        }

        if (value == 1) {
            log.error("正在浇花中");
            return R.build(new BizException(BizResponseCode.ERR_400, "正在浇花中，请稍后"));
        }

        redisService.save(WATERFLOWER_KEY, 1);
        return R.ok();
    }

    /**
     * 获取浇花指令 1表式浇花 0表示不用浇
     *
     * @return R
     */
    @GetMapping("/get/water/flower/cmd")
    public R<Integer> getWaterFlowerCMD() {
        Integer value = (Integer) redisService.find(WATERFLOWER_KEY);
        if (value == 0) {
            return R.build(new BizException(BizResponseCode.ERR_400));
        }
        redisService.save(WATERFLOWER_KEY, 0);
        return R.ok(value);
    }

    /**
     * 设置施肥指令
     *
     * @return R
     */
    @GetMapping("/set/feed/flower/cmd")
    @Operation(summary = "设置施肥指令")
    public R<Void> setFeedFlowerCMD() {
        Integer value = (Integer) redisService.find(FEEDFLOWER_KEY);
        if (value == null) {
            redisService.save(WATERFLOWER_KEY, 0);
            value = 0;
        }

        if (value == 1) {
            log.error("正在施肥中");
            return R.build(new BizException(BizResponseCode.ERR_400, "正在施肥中，请稍后"));
        }

        redisService.save(FEEDFLOWER_KEY, 1);
        return R.ok();
    }

    /**
     * 获取施肥指令 1表式施肥 0表示不用施肥
     *
     * @return R
     */
    @GetMapping("/get/feed/flower/cmd")
    @Operation(summary = "获取施肥指令")
    public R<Integer> getFeedFlowerCMD() {
        Integer value = (Integer) redisService.find(FEEDFLOWER_KEY);
        if (value == 0) {
            return R.build(new BizException(BizResponseCode.ERR_400));
        }
        redisService.save(FEEDFLOWER_KEY, 0);
        return R.ok(value);
    }

    /**
     * 设置浇花时间
     *
     * @param min 秒
     * @return R
     */
    @PostMapping("/set/water/flower/time")
    @Operation(summary = "设置浇花时间")
    public R<Map<String, Object>> setWaterFlowerTime(@RequestParam("min") Integer min) {

        Map<String, Object> map = new HashMap<>();

        map.put("time", min);
        map.put("status", 1);

        redisService.save(WATERFLOWERTIME_KEY, map);
        return R.ok();
    }

    /**
     * 设置施肥时间
     *
     * @param second 秒
     * @return R
     */
    @PostMapping("/set/feed/flower/time")
    @Operation(summary = "设置施肥时间")
    public R<Map<String, Object>> setFeedFlowerTime(@RequestParam("second") Integer second) {

        Map<String, Object> map = new HashMap<>();

        map.put("time", second);
        map.put("status", 1);

        redisService.save(FEEDFLOWERTIME_KEY, map);
        return R.ok();
    }

    /**
     * 设置土壤阈值
     *
     * @param value 土壤阈值  1-100
     * @return R
     */
    @PostMapping("/set/soil/threshold")
    public R<Map<String, Object>> setSoilThreshold(@RequestParam("value") Integer value) {

        Map<String, Object> map = new HashMap<>();
        map.put("value", value);
        map.put("status", 1);
        redisService.save(SOIL_THRESHOLD_KEY, map);

        return R.ok();
    }

    /**
     * 获取浇花时间
     *
     * @return R
     */
    @GetMapping("/get/water/flower/time")
    @Operation(summary = "获取浇花时间")
    public R<Map<String, Object>> getWaterFlowerTime() {

        Map<String, Object> get = (Map<String, Object>) redisService.find(WATERFLOWERTIME_KEY);
        log.info("get: {}", get);
        if (get == null || get.get("time") == null) {
            return R.build(new BizException(BizResponseCode.ERR_400));
        }

        Map<String, Object> data = new HashMap<>();
        data.put("time", get.get("time"));
        data.put("status", 0);

        redisService.save(WATERFLOWERTIME_KEY, data);

        return R.ok(get);
    }

    /**
     * 获取施肥时间
     *
     * @return R
     */
    @GetMapping("/get/feed/flower/time")
    @Operation(summary = "获取施肥时间")
    public R<Map<String, Object>> getFeedFlowerTime() {

        Map<String, Object> get = (Map<String, Object>) redisService.find(FEEDFLOWERTIME_KEY);
        if (get == null || get.get("time") == null) {
            return R.build(new BizException(BizResponseCode.ERR_400));
        }

        Map<String, Object> data = new HashMap<>();
        data.put("time", get.get("time"));
        data.put("status", 0);

        redisService.save(FEEDFLOWERTIME_KEY, data);
        return R.ok(get);
    }

    /**
     * 获取土壤阈值
     *
     * @return R
     */
    @GetMapping("/get/soil/threshold")
    @Operation(summary = "获取土壤阈值")
    public R<Map<String, Object>> getSoilThreshold() {

        Map<String, Object> get = (Map<String, Object>) redisService.find(SOIL_THRESHOLD_KEY);
        if (get == null || get.get("value") == null) {
            return R.build(new BizException(BizResponseCode.ERR_400));
        }

        Map<String, Object> data = new HashMap<>();
        data.put("value", get.get("value"));
        data.put("status", 0);

        redisService.save(SOIL_THRESHOLD_KEY, data);
        return R.ok(get);
    }




    @PostConstruct
    public void init() {
        redisService.save(SELECT_FLOWER_KEY, 1);
        redisService.save(ACTION_KEY, 0);
        redisService.save(WATERFLOWER_KEY, 0);
        redisService.save(FEEDFLOWER_KEY, 0);
    }

}
